<template>
  <div>
    <header class="login-header">
      <div class="container m-top-20">
        <h1 class="logo">
          <RouterLink to="/">小兔鲜</RouterLink>
        </h1>
        <RouterLink class="entry" to="/">
          进入网站首页
          <i class="iconfont icon-angle-right"></i>
          <i class="iconfont icon-angle-right"></i>
        </RouterLink>
      </div>
    </header>
    <section class="login-section">
      <div class="wrapper">
        <nav>
          <a href="javascript:;">注册账号</a>
        </nav>
        <div class="name-box">
            <div class="form">
                <el-form ref="formRef" :model="ruleForm" :rules="rules" label-width="100px"  status-icon>
                   <el-form-item prop="name" label="用户名">
                  <el-input placeholder="请输入用户名"  size="mini" v-model="ruleForm.name" />
                </el-form-item>
                <el-form-item label="手机号" prop="phone">
                    <el-input placeholder="请输入手机号" size="mini" v-model="ruleForm.phone" />
                </el-form-item>
<!--
                 <el-form-item label="性别" prop="phone">

                     <el-radio v-model="ruleForm.gender" label="男">男</el-radio>
                      <el-radio v-model="ruleForm.gender" label="女">女</el-radio>
                </el-form-item> -->

                    <el-form-item prop="password" label="密码">
                           <el-input size="mini"  placeholder="请输入密码" type="password" v-model="ruleForm.password" show-password/>
                    </el-form-item>

                    <el-form-item prop="checkPass" label="确认密码">
                           <el-input size="mini"  placeholder="请再次输入密码" type="password" v-model="ruleForm.checkPass" show-password/>
                    </el-form-item>

                    <el-form-item prop="postbox" label="邮箱">
                           <el-input size="mini"  placeholder="请输入邮箱" v-model="ruleForm.postbox" />
                    </el-form-item>
                     <el-form-item label="验证码" prop="captcha">
                        <div class="captcha-box">
                            <el-input size="mini" placeholder="请输入验证码" style="width: 200px" v-model="ruleForm.captcha" />
                            <canvas ref="captchaCanvas" class="captcha-canvas" @click="generateCaptcha"></canvas>
                        </div>
                    </el-form-item>
                    <el-form-item prop="agree" label-width="22px">
                        <el-checkbox v-model="ruleForm.agree" size="large">
                         我已同意隐私条款和服务条款
                        </el-checkbox>
                    </el-form-item>
                    <el-button size="large" class="subBtn" @click="dologin" >注册</el-button>
                </el-form>

            </div>
        </div>
      </div>
    </section>

    <footer class="login-footer">
      <div class="container">
        <p>
          <a href="javascript:;">关于我们</a>
          <a href="javascript:;">帮助中心</a>
          <a href="javascript:;">售后服务</a>
          <a href="javascript:;">配送与验收</a>
          <a href="javascript:;">商务合作</a>
          <a href="javascript:;">搜索推荐</a>
          <a href="javascript:;">友情链接</a>
        </p>
        <p>CopyRight &copy; javaWeb项目期末</p>
      </div>
    </footer>
  </div>
</template>

<script>
import { ReigestService } from '@/api/user'
import { removeToken, getToken } from '@/utils/auth'
export default {
  name: 'LoginIndex',
  data () {
    return {
    //   captcha: '', // 验证码
      // 注册账号
      ruleForm: {
        name: 'yuanwu',
        password: '123456',
        checkPass: '123456',
        postbox: '3079145685@qq.com',
        phone: '17381752915',
        gender: '男',
        captcha: '', // 验证码
        agree: true
      },
      // 表单验证规则
      rules: {
        name: [
          { required: true, message: '请输入账户', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        // 手机号
        phone: [
          { required: true, message: '请输入手机号', trigger: 'change' },
          { pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
        ],
        // 密码重复校验
        checkPass: [
          { required: true, message: '请再次输入密码', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              if (value === '') {
                callback(new Error('请再次输入密码'))
              } else if (value !== this.ruleForm.password) {
                callback(new Error('两次输入密码不一致!'))
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }
        ],
        // 验证码
        captcha: [
          { required: true, message: '请输入验证码', trigger: 'change' },
          {
            validator: (rule, value, callback) => {
              if (value.length === 4) {
                if (value.toLowerCase() !== this.captcha.toLowerCase()) {
                  callback(new Error('验证码不正确'))
                } else {
                  callback()
                }
              } else {
                callback()
              }
            },
            trigger: 'blur'
          },
          { min: 4, max: 4, message: '验证码长度为4位', trigger: 'blur' }
        ],
        // 邮箱校验规则
        postbox: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
        ],
        agree: [
          { required: true, message: '请同意隐私条款和服务条款', trigger: 'change' },
          {
            validator: (rule, value, callback) => {
              if (value) {
                callback() // 如果勾选了，校验通过
              } else {
                callback(new Error('请勾选服务条款')) // 如果没有勾选，提示错误
              }
            }
          }
        ]
      }
    }
  },
  methods: {
    // 生成验证码
    generateCaptcha () {
      const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
      let captchaStr = ''
      for (let i = 0; i < 4; i++) {
        captchaStr += chars.charAt(Math.floor(Math.random() * chars.length))
      }
      this.captcha = captchaStr

      // 获取 canvas 上下文
      const canvas = this.$refs.captchaCanvas
      const ctx = canvas.getContext('2d')
      const width = canvas.width = 120
      const height = canvas.height = 40

      // 背景色
      ctx.fillStyle = '#f0f0f0'
      ctx.fillRect(0, 0, width, height)

      // 绘制验证码字符
      ctx.font = 'bold 24px Courier New'
      ctx.fillStyle = '#333'
      ctx.textBaseline = 'middle'
      for (let i = 0; i < captchaStr.length; i++) {
        const x = 20 + i * 25
        const y = height / 2 + Math.random() * 10 - 5 // 随机偏移位置
        ctx.fillText(captchaStr[i], x, y)
      }

      // 添加干扰线
      for (let i = 0; i < 5; i++) {
        ctx.strokeStyle = '#dcdcdc'
        ctx.beginPath()
        ctx.moveTo(Math.random() * width, Math.random() * height)
        ctx.lineTo(Math.random() * width, Math.random() * height)
        ctx.lineWidth = 1
        ctx.stroke()
      }

      // 添加干扰点
      for (let i = 0; i < 30; i++) {
        ctx.fillStyle = '#dcdcdc'
        ctx.beginPath()
        ctx.arc(Math.random() * width, Math.random() * height, 1, 0, Math.PI * 2, false)
        ctx.fill()
      }
    },
    dologin () {
      // 校验表单
      this.$refs.formRef.validate(async (ok) => {
        if (ok) {
          const data = {
            name: this.ruleForm.name,
            password: this.ruleForm.name,
            gender: this.ruleForm.gender,
            postbox: this.ruleForm.postbox,
            phone: this.ruleForm.phone,
            city: ''
          }
          try {
            const res = await ReigestService(data)
            console.log('注册结果', res)
          } catch (err) {
            this.$message.error('用户已存在，注册失败')
          }
          // this.ruleForm = {
          //   name: '',
          //   password: '',
          //   checkPass: '',
          //   postbox: '',
          //   phone: '',
          //   captcha: '', // 验证码
          //   agree: false
          // }
        } else {
          this.$message.error('请填写完整表单内容')
        }
      })
    }
  },
  // 在组件加载时生成一个验证码
  mounted () {
    if (getToken()) {
      removeToken()
      location.reload()
    }
    this.generateCaptcha()
  }
}
</script>

<style scoped lang='scss'>
.login-header {
  background: #fff;
  border-bottom: 1px solid #e4e4e4;

  .container {
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
  }

  .logo {
    width: 200px;

    a {
      display: block;
      height: 132px;
      width: 100%;
      text-indent: -9999px;
      background: url("@/assets/images/logo.png") no-repeat center 18px / contain;
    }
  }

  .sub {
    flex: 1;
    font-size: 24px;
    font-weight: normal;
    margin-bottom: 38px;
    margin-left: 20px;
    color: #666;
  }

  .entry {
    width: 120px;
    margin-bottom: 38px;
    font-size: 16px;

    i {
      font-size: 14px;
      color: #27ba9b;
      letter-spacing: -5px;
    }
  }
}

.login-section {
  background: url('@/assets/images/login-bg.png') no-repeat center / cover;
  height: 488px;
  position: relative;

  .wrapper {
    width: 500px;
    background: #fff;
    position: absolute;
    left: 23%;
    top: -80px;
    transform: translate3d(100px, 0, 0);
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.15);
        .captcha-box {
        display: flex;
        align-items: center;
        }

        .captcha-canvas {
        width: 120px;
        height: 35px;
        margin-left: 50px;
        border: 1px solid #ccc;
        margin-right: 10px;
        cursor: pointer;
        }
    nav {
      font-size: 14px;
      height: 55px;
      margin-bottom: 20px;
      border-bottom: 1px solid #f5f5f5;
      display: flex;
      padding: 0 40px;
      text-align: right;
      align-items: center;

      a {
        flex: 1;
        line-height: 1;
        display: inline-block;
        font-size: 18px;
        position: relative;
        text-align: center;
      }
    }
  }
}

.login-footer {
  padding: 30px 0 50px;
  background: #fff;
  p {
    text-align: center;
    color: #999;
    padding-top: 50px;

    a {
      line-height: 1;
      padding: 0 10px;
      color: #999;
      display: inline-block;

      ~a {
        border-left: 1px solid #ccc;
      }
    }
  }
}

.name-box {
  .toggle {
    padding: 15px 40px;
    text-align: right;

    a {
      color: #27ba9b;

      i {
        font-size: 14px;
      }
    }
  }
  .form {
    padding: 0 20px 20px 20px;

    &-item {
      margin-bottom: 28px;

      .input {
        position: relative;
        height: 36px;

        >i {
          width: 34px;
          height: 34px;
          background: #cfcdcd;
          color: #fff;
          position: absolute;
          left: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 18px;
        }

        input {
          padding-left: 44px;
          border: 1px solid #cfcdcd;
          height: 36px;
          line-height: 36px;
          width: 100%;

          &.error {
            border-color: #cf4444;
          }

          &.active,
          &:focus {
            border-color: #27ba9b;
          }
        }

        .code {
          position: absolute;
          right: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 14px;
          background: #f5f5f5;
          color: #666;
          width: 90px;
          height: 34px;
          cursor: pointer;
        }
      }

      >.error {
        position: absolute;
        font-size: 12px;
        line-height: 28px;
        color: #cf4444;

        i {
          font-size: 14px;
          margin-right: 2px;
        }
      }
    }

    .agree {
      a {
        color: #069;
      }
    }

    .btn {
      display: block;
      width: 100%;
      height: 40px;
      color: #fff;
      text-align: center;
      line-height: 40px;
      background: #27ba9b;

      &.disabled {
        background: #cfcdcd;
      }
    }
  }

  .action {
    padding: 20px 40px;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .url {
      a {
        color: #999;
        margin-left: 10px;
      }
    }
  }
}

.subBtn {
    cursor: pointer;
  background: #27ba9b;
  width: 30%;
  margin-left: 35%;
  color: #fff;
}
</style>
